package com.rms.controller;

import com.rms.common.Const;
import com.rms.entity.DinnerTable;
import com.rms.service.IDinnerTableService;

import com.rms.utils.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


import javax.annotation.Resource;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by cq on 2017/5/28.
 */

@Controller
@RequestMapping(value = "/table")
public class DinnerTableAction extends BaseAction {




    @RequestMapping(value="/search")
    public String search(Model model,String keyword,HttpServletRequest request){
        if(StringUtils.isNull(keyword))
            return list(model,request);

        List<DinnerTable> list = dinnerTableService.query(keyword);
        model.addAttribute("list",list);
        return "/sys/board/boardList.jsp";
    }


    @RequestMapping(value="/list")
    public String list(Model model,HttpServletRequest request){


        List<DinnerTable> list = dinnerTableService.query();
        model.addAttribute("list",list);

        //将餐桌列表存到context里传到前台显示
        request.getServletContext().setAttribute("table", list);

        return "/sys/board/boardList.jsp";
    }


    @RequestMapping(value = "/delete")
    private String delete(int id,Model model,HttpServletRequest request)
            throws ServletException, IOException {
        dinnerTableService.delete(id);
       return list(model,request);
    }

    @RequestMapping(value = "/update")
    private String update(int id,Model model,HttpServletRequest request)
            throws ServletException, IOException{

        dinnerTableService.changeState(id);
        return  list(model,request);
    }


    @RequestMapping(value = "/add")
    private String add(Model model,String tableName,HttpServletRequest request)
            throws ServletException, IOException{

        if(tableName==null || "".equals(tableName.trim()))
            return list(model,request);


        DinnerTable dt = new DinnerTable();
        dt.setTableName(tableName);
        dinnerTableService.add(dt);
        return list(model,request);
    }

}
